package com.itic.ht.cms.examrecord.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.code.vo.CodeDetailVo;
import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.ht.cms.exam.service.ExamService;
import com.itic.ht.cms.examrecord.persistence.model.ExamQuestion;
import com.itic.ht.cms.examrecord.persistence.model.ExamQuestionExample;
import com.itic.ht.cms.examrecord.persistence.model.ExamRecord;
import com.itic.ht.cms.examrecord.persistence.model.ExamRecordExample;
import com.itic.ht.cms.examrecord.persistence.model.ExamRecordExample.Criteria;
import com.itic.ht.cms.examrecord.service.ExamQuestionService;
import com.itic.ht.cms.examrecord.service.ExamRecordService;
import com.itic.ht.cms.question.persistence.model.QuestionModel;
import com.itic.ht.cms.question.persistence.model.QuestionModelExample;
import com.itic.ht.cms.questype.service.QuestionTypeService;

@Controller
@RequestMapping(value = "/cms/examrecord")
public class ExamRecordController extends BaseController {

	@Autowired
	private ExamRecordService examRecordService;
	@Autowired
	private ExamService examService;
	@Autowired
	private ExamQuestionService examQuestionService;
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired

	////////////////////////////////////////////////////// 常量区////////////////////////////////////////////////////////////////
	private static final String MODEL_NAME_CHINA = "考试记录";// 中文名
	private static final String MODEL_NAME＿ENGLISH = "examrecord";// 英文名
	private static final String PAGE_COMMON_PATH = "/cms/examrecord/examrecord";// 通用路径
	private static final String COMMON_URL = "/cms/examrecord/";// 通用路径
	// 返回属性黑名单
	private static String[] ignoreProperties = new String[] { "mtime" };

	////////////////////////////////////////////////////// 页面跳转区////////////////////////////////////////////////////////////

	/*
	 * 主界面
	 */
	@RequestMapping(value = "/init")
	public String init(Model model) {
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Main";
	}

	/*
	 * 添加页面
	 */
	@RequestMapping(value = "/initAdd")
	public String initAdd(Model model) {
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Add";
	}

	/*
	 * 详情页面
	 */
	@RequestMapping(value = "/initView")
	public String initView(String id,String account, Model model) {
		if (StringHelper.isNotBlank(id)) {
			model.addAttribute("name_china", MODEL_NAME_CHINA);
			model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
			model.addAttribute("common_url", COMMON_URL);
			model.addAttribute("id", id);
			model.addAttribute("account", account);
		} else {
			throw new ParameterErrorException();
		}
		return PAGE_COMMON_PATH + "View";
	}

	@RequestMapping(value = "/initList")
	@ResponseBody
	public Map<String, Object> initView(String id,String account, HttpServletRequest request) {
		// 声明返回对象
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (StringHelper.isNotBlank(id)) {
			ExamRecord examRecord = examRecordService.get(id);
			ExamQuestionExample example = new ExamQuestionExample();
			example = this.handlePageQuery(request, example);
			if(null!=examRecord){
				example.createCriteria().andExamIdEqualTo(examRecord.getExamId());
			}
			// 封装分页参数
			example.createCriteria().andAccountEqualTo(account);
			example.setOrderByClause("CTIME desc");
			example.setOrderByClause("REORDER asc");
			// 获取信息总条数
			int total = examQuestionService.count(example);

			// 获取数据
			List<ExamQuestion> list = examQuestionService.list(example);

			// 结果集
			if (list != null && list.size() > 0) {
				for (ExamQuestion temp : list) {
					temp.setExamId(examService.getExamName(temp.getExamId()).getExamName());
					if ("1".equals(temp.getMemberAnswer())) {
						temp.setMemberAnswer("正确");
					} else {
						temp.setMemberAnswer("错误");
					}
				}
				// 过滤黑名单字段
			}

			// 数据返回处理
			resultMap.put(IticConst.TOTAL_RECORDS, total);
			resultMap.put(IticConst.DISPLAY_RECORDS, total);
			resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
			resultMap.put(IticConst.DATA, list);
		}

		return resultMap;

	}

	/**
	 * 添加模板管理数据
	 * 
	 * @param android
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, String> add(ExamRecord templet) {

		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		result = examRecordService.add(templet);
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, String> del(String ids) {
		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		if (StringHelper.isNotBlank(ids)) {
			ExamRecord temp = new ExamRecord();
			temp.setId(ids);
			temp.setDelFlag(Integer.parseInt(DBConst.FALSE));
			result = examRecordService.delete(temp);
		}

		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(ExamRecord templet, HttpServletRequest request) {
		// 声明返回对象
		Map<String, Object> resultMap = new HashMap<String, Object>();

		ExamRecordExample example = this.queryCondition(request, templet);
		// 封装分页参数
		example = this.handlePageQuery(request, example);

		// 获取信息总条数
		int total = examRecordService.count(example);

		// 获取数据
		List<ExamRecord> list = examRecordService.list(example);

		// 结果集
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if (list != null && list.size() > 0) {
			for (ExamRecord temp : list) {
				temp.setCategoryId(questionTypeService.getTypeName(temp.getCategoryId()).getName());
				temp.setExamId(examService.getExamName(temp.getExamId()).getExamName());
				if ("1".equals(temp.getExamResult())) {
					temp.setExamResult("通过");
				} else {
					temp.setExamResult("不通过");
				}
			}
			// 过滤黑名单字段
			resultList = ValueHelper.tranList2MapList(list, ignoreProperties);
		}

		// 数据返回处理
		resultMap.put(IticConst.TOTAL_RECORDS, total);
		resultMap.put(IticConst.DISPLAY_RECORDS, total);
		resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
		resultMap.put(IticConst.DATA, resultList);
		return resultMap;

	}

	private ExamRecordExample queryCondition(HttpServletRequest request, ExamRecord templet) {
		// 参数处理
		ExamRecordExample example = new ExamRecordExample();
		// 按照创建时间倒叙
		example.setOrderByClause("ctime desc");
		// 查询条件
		Criteria criteria = example.createCriteria().andDelFlagEqualTo(1);
		if (null != templet) {
			// 用户账号查询
			if (StringHelper.isNotBlank(templet.getAccount())) {
				criteria.andAccountEqualTo(templet.getAccount());
			}

			// 试卷类型查询
			if (StringHelper.isNotBlank(templet.getCategoryId())) {
				criteria.andCategoryIdEqualTo(templet.getCategoryId());
			}
			// 考试结果查询
			if (StringHelper.isNotBlank(templet.getExamResult())) {
				criteria.andExamResultEqualTo(templet.getExamResult());
			}
		}
		return example;
	}

	/**
	 * 获取代码表信息 type :代码表类型，如T_DM_SEX filterCode :需要过滤的代码
	 * 
	 * @param codeDetailVo
	 * @return
	 */
	@RequestMapping(value = "/getCodeDetail")
	@ResponseBody
	public List<CodeDetailVo> getCodeDetail(CodeDetailVo codeDetailVo) {
		// 声明返回对象
		List<CodeDetailVo> codeDetailVoList = new ArrayList<CodeDetailVo>();
		// 封装数据
		CodeDetailVo vo = new CodeDetailVo();
		vo.setId("1");
		vo.setCode("1");
		vo.setName("通过");
		codeDetailVoList.add(vo);
		vo = new CodeDetailVo();
		vo.setId("0");
		vo.setCode("0");
		vo.setName("不通过");
		codeDetailVoList.add(vo);
		return codeDetailVoList;
	}
}
